package nio.bytebuffer;

import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;


public class Test {
    public static void main(String[] args) {
        try (FileChannel channel = new FileInputStream("data.txt").getChannel()){
            ByteBuffer buffer = ByteBuffer.allocate(10);
            while (true) {
                int len = channel.read(buffer);
                System.out.println("读取到的字节数 " + len);
                if (len == -1) {
                    break;
                }
                buffer.flip();
                while (buffer.hasRemaining()) {
                    byte b = buffer.get();
                    System.out.println("实际字节 " + (char)b);
                }
                buffer.clear();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
